#!/usr/bin/python
# -*- coding:utf-8 -*-
# @author  : Chestnut
# @time    : 2025/6/30 09:06
# @function: What can I do.
# @version : V1
import tkinter
from tkinter import filedialog


class GetColumn(tkinter.Frame):
    def __init__(self):
        self.RED = '\033[91m'
        self.GREEN = '\033[92m'
        self.END = '\033[0m'

    def read_file(self):
        try:
            self.file.close()
        except:
            pass
        askfile = filedialog.askopenfilename(filetypes=[
            ("txt Files", "*.txt"),
            ("seabird capture Files", "*.cap"),
            ("All Files", "*.*")
        ])
        if askfile != "":
            f1 = open(askfile, "r")
            self.file = f1.readlines()
            self.file_name.set(askfile)
            self.showFirstRow()  # 注意！！！！！！！！！！！！！！！！这里有自动进入
        else:
            self.file = None
            self.file_name.set(askfile)
            pass

    def showFirstRow(self):
        self.t1.delete("1.0", tkinter.END)
        self.t1.insert(tkinter.END, "当前文件：" + self.file_name.get() + "\n")
        self.t1.insert(tkinter.END, "序号\t示例\n")
        for i in range(len(self.file[0].split())):
            self.t1.insert(tkinter.END, str(i) + '\t' + self.file[0].split()[i] + "\n")
        self.t1.insert(tkinter.END, "请输入对应的序号\n")

    def splitFile(self, event=None):
        self.result = ""
        for row in self.file:
            for col_index in self.e0.get().split():
                self.result += row.split()[int(col_index)]
            self.result += "\n"
        self.t1.insert(tkinter.END, "开始分割\n")
        self.t1.insert(tkinter.END, self.result)
        self.t1.see(tkinter.END)
        self.write_file()  # 注意！！！！！！！！！！！！！！！！！！！！！！！！！！！！这里有自动进入
        # self.t1.insert(tkinter.END, "请输入对应的序号")

    def write_file(self):
        target_file = self.file_name.get()
        if target_file != "":
            if target_file.endswith(".txt"):
                t_f = open(target_file, "w", encoding="utf-8")
                t_f.write(self.result)
                t_f.close()
                self.t1.insert(tkinter.END, "转换完成")
                self.t1.see(tkinter.END)
            else:
                t_f = open(target_file + '.txt', "w", encoding="utf-8")
                t_f.write(self.result)
                t_f.close()
                self.t1.insert(tkinter.END, "转换完成")
                self.t1.see(tkinter.END)
        else:
            self.t1.insert(tkinter.END, "保存文件未选择")
            self.t1.see(tkinter.END)
            pass

    def mainWindow(self):
        root = tkinter.Tk()
        root.title("Get Column")
        root.resizable(False, False)

        self.file_name = tkinter.StringVar()
        self.file_name.set("")

        root.bind("<Return>", self.splitFile)
        # root.bind_all("<Control-s>", self.write_file)
        # 第一行
        Frm0 = tkinter.Frame(root)
        tkinter.Label(Frm0, text="选择文件").pack(side=tkinter.LEFT)
        tkinter.Entry(Frm0, state="disabled", textvariable=self.file_name, width=70).pack(fill=tkinter.X)
        Frm0.pack(side=tkinter.TOP, fill=tkinter.X)
        # 第二行（输入）
        Frm1 = tkinter.Frame(root)
        tkinter.Label(Frm1, text="请输入下方序号").pack(side=tkinter.LEFT, padx=5)
        self.e0 = tkinter.Entry(Frm1)
        self.e0.pack(side=tkinter.LEFT, padx=5, fill=tkinter.X, expand=True)
        Frm1.pack(side=tkinter.TOP, fill=tkinter.X)
        # 第三行（指令）
        Frm2 = tkinter.Frame(root)
        tkinter.Button(Frm2, text="Browse", command=self.read_file).pack(side=tkinter.LEFT, padx=5, ipadx=10,
                                                                         anchor=tkinter.CENTER, expand=True)
        # tkinter.Button(Frm2, text="Show", command=self.showFirstRow).pack(side=tkinter.LEFT, padx=5, ipadx=10,
        #                                                                   anchor=tkinter.CENTER, expand=True)
        tkinter.Button(Frm2, text="Split", command=self.splitFile).pack(side=tkinter.LEFT, padx=5, ipadx=10,
                                                                        anchor=tkinter.CENTER, expand=True)
        tkinter.Button(Frm2, text="Save", command=self.write_file).pack(side=tkinter.LEFT, padx=5, ipadx=10,
                                                                        anchor=tkinter.CENTER, expand=True)
        Frm2.pack(side=tkinter.TOP, fill=tkinter.X)
        # 第四行（输出）
        Frm3 = tkinter.Frame(root)
        tkinter.Label(Frm3, text="预览").pack(side=tkinter.TOP, fill=tkinter.X)
        self.t1 = tkinter.Text(Frm3, height=20)
        self.t1.pack(side=tkinter.LEFT, fill=tkinter.BOTH, expand=True)
        scb = tkinter.Scrollbar(Frm3, command=self.t1.yview)
        scb.pack(side=tkinter.LEFT, fill=tkinter.Y)
        self.t1.configure(yscrollcommand=scb.set)
        Frm3.pack(side=tkinter.TOP, fill=tkinter.X)

        root.mainloop()


myapp = GetColumn()
myapp.mainWindow()
